package com.ktg.mes.pro.domain;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.annotation.Excel;
import com.ktg.common.core.domain.BaseEntity;
import com.ktg.common.utils.DateUtils;
import com.ktg.mes.pro.service.IProRouteProcessService;
import com.ktg.mes.rec.domain.RecProcessesExt;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 工艺组成对象 pro_route_process
 *
 * @author yinjinlu
 * @date 2022-05-13
 */

@Data
public class ProRouteProcessData extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 记录ID */
    private Long recordId;

    /** 工序id */
    @Excel(name = "工序id")
    private Long processId;

    /** 工序名称 */
    @Excel(name = "工序名称")
    private String processName;


    /** 序号 */
    @Excel(name = "序号")
    private Integer orderNum;
    private Integer groupNum;


    /**
     * 工艺配置参数
     */
    private String paramJson;



    /**
     * 工作类型 1正常 2返工
     */
    private Double workType;


    /**
     * PDA 采集参数
     */
    private String dicText;

    /**
     * 设备上传数据，开始计算时间
     */
    private Date startTime;

    /**
     * 设备上传温度最大值
     */
    private Double wenDu;


    /**
     * 设备上传总时长
     */
    private Integer shiChang;

    /**
     * 设备编码
     */
    private String machineryCode;

    /**
     * 桶号
     */
    private String toolTypeCode;


    /**
     * 那些内容不需要打印到 操作要求
     * @param key
     * @return
     */
    private boolean isNotPrintRequirements(String key){
        if (StringUtils.isEmpty(key)){
            return true;
        }
        List<String> list = new ArrayList<>();
        list.add("录入完工");
        list.add("录入检测结果");
        for (int i = 0;i < list.size();i++){
            if (list.get(i).equals(key)){
                return true;
            }
        }
        return false;
    }


    /**
     * 那些内容不需要打印到 作业时间和操作过程
     * @param key
     * @return
     */
    private boolean isNotPrintProcess(String key){
        if (StringUtils.isEmpty(key)){
            return true;
        }
        List<String> list = new ArrayList<>();
        list.add("录入完工");
        list.add("生产完成");
        list.add("转速");
        list.add("排油时间");
        for (int i = 0;i < list.size();i++){
            if (list.get(i).equals(key)){
                return true;
            }
        }
        return false;
    }

    /**
     *
     * @param list
     * @param fenShanIndex 第几次分散
     * @param yanMoIndex 第几次研磨
     * @param remark
     * @return
     */
    public Map<String,String> dealTechnologicalProcessData(List<RecProcessesExt> list,String remark, int fenShanIndex,int yanMoIndex){
        Map<String,String> map = new HashMap<>();

        String row1 = String.valueOf(groupNum);
        String row2 = processName;
        if (workType != null && workType.equals("2")){
            row2 = "返:"+processName;
        }
        JSONObject jsonObject = null;
        if (!StringUtils.isEmpty(paramJson)) {
            jsonObject = JSONObject.parseObject(paramJson);
        }
        String row3 = row3(row2,list,remark,jsonObject,fenShanIndex);

        String row4 = "";
        // 第四行，PDA 数据和后台数据
        if (startTime != null){
            String value = DateUtils.parseDateToStr("yyyy:MM:dd HH:mm",startTime);
            if (StringUtils.isEmpty(row4)){
                row4 = "开始时间 : "+value;
            }else {
                row4 += "\n开始时间 : "+value;
            }
        }
        if (shiChang != null){
            String value = "时长:"+String.format("%.0f",shiChang/60.0)+"(分钟)";
            if (StringUtils.isEmpty(row4)){
                row4 = value;
            }else {
                row4 += "\n"+value;
            }
        }
        if (wenDu != null){
            String value = "温度≤"+wenDu+"(℃)";
            if (StringUtils.isEmpty(row4)){
                row4 = value;
            }else {
                row4 += "\n"+value;
            }
        }


        if (!StringUtils.isEmpty(dicText)){
            JSONObject jsonObj = JSONObject.parseObject(dicText);
            for (int i = 0;i < list.size();i++){
                String key = list.get(i).getNamePda();
                String value = jsonObj.getString(key);
                jsonObj.remove(key);
                if (isNotPrintProcess(key)){
                    continue;
                }
                if (StringUtils.isEmpty(value)){
                    continue;
                }
                if (StringUtils.isEmpty(row4)){
                    row4 = key+" : "+value;
                }else {
                    row4 += "\n"+key+" : "+value;
                }
            }
            //处理那些自定义的key - value
            if (jsonObj.size() > 0){
                Set<String> keySet = jsonObj.keySet();
                for (String key:keySet){
                    String value = jsonObj.getString(key);
                    if (isNotPrintProcess(key)){
                        continue;
                    }
                    if (StringUtils.isEmpty(value)){
                        continue;
                    }
                    if (StringUtils.isEmpty(row4)){
                        row4 = key+" : "+value;
                    }else {
                        row4 += " , "+key+" : "+value;
                    }
                }
            }
        }
        String row5 = "";
        if (machineryCode != null){
            row5 = machineryCode;
        }

        String row6 = "";
        if (toolTypeCode != null){
            row6 = toolTypeCode;
        }

        String row2Str = row2(row2,fenShanIndex,yanMoIndex);
        String row4Str = row4(row2Str,row4,fenShanIndex,jsonObject);
        String row7 = getCreateBy();
        toMap(map,"row1",row1);
        toMap(map,"row2",row2Str);
        toMap(map,"row3",row3);
        toMap(map,"row4",row4Str);
        toMap(map,"row5",row5);
        toMap(map,"row6",row6);
        toMap(map,"row7",row7);
        toMap(map,"分散",isKey(row2,"分散",fenShanIndex));
        toMap(map,"研磨",isKey(row2,"研磨",yanMoIndex));

        return map;
    }

    //判断是分散研磨工序,需要添加序号
    public String row2(String row2, int fenShanIndex,int yanMoIndex){
        if (row2.equals("分散")){
            return  row2+fenShanIndex;
        }
        if (row2.equals("研磨")){
            if (yanMoIndex == 1){
                return row2+"一";
            }else if(yanMoIndex == 2){
                return row2+"二";
            }else if(yanMoIndex == 3){
                return row2+"三";
            }

        }
        return row2;
    }


    //判断是否是分散研磨工序,返回 index+1
    public String isKey(String row2,String key, int index){
        if (row2.equals(key)){
            return String.valueOf(index+1);
        }
        return String.valueOf(index);
    }

    private String row3(String row2,List<RecProcessesExt> list,String remark,JSONObject jsonObject,int fenShanIndex) {
        String row3 = "";
        if (jsonObject != null){
            for (int i = 0;i < list.size();i++){
                String key = list.get(i).getNameBackstage();
                if (isNotPrintRequirements(key)){
                    continue;
                }
                String value = jsonObject.getString(key);
                if (StringUtils.isEmpty(value)){
                    continue;
                }
                if (StringUtils.isEmpty(row3)){
                    if (!value.equals("N")){
                        if (value.equals("Y")){
                            row3 = key;
                        }else {
                            if (key.equals("后加物料")){
                                row3 = value.replace("[\"","")
                                        .replace("\"]","")
                                        .replace("\",\"","_____\n")+"_____";
                                if (fenShanIndex > 3){
                                    row3 += "\n调粘：";
                                }
                            }else {
                                row3 = key.replace(processName,"")+value;
                            }
                        }
                    }
                }else {
                    if (!value.equals("N")) {
                        if (key.equals("后加物料")){
                            row3 += "\n"+value.replace("[\"","")
                                    .replace("\"]","")
                                    .replace("\",\"","_____\n")+"_____";
                            if (fenShanIndex > 3){
                                row3 += "\n调粘：";
                            }
                        }else {
                            row3 += "\n" + key.replace(processName,"") + value;
                        }
                    }
                }
            }
        }

        if (!StringUtils.isEmpty(remark)){
            if (StringUtils.isEmpty(row3)){
                row3 = "备注："+remark;
            }else {
                row3 += "\n备注："+remark;
            }
        }
        return row3;
    }

    private String row4(String row2, String row4,int fenShanIndex,JSONObject jsonObject){
        Map<String,String> map = new HashMap<>();
        String nullDateStr = "__月 __日 __时 __分 -- __月 __日 __时 __分";
        map.put("加粉",nullDateStr);
        map.put("分散1",nullDateStr);
        map.put("研磨一",nullDateStr);
        map.put("分散2",nullDateStr);
        map.put("研磨二",nullDateStr);
        map.put("分散3",nullDateStr);
        map.put("后补溶剂","___时___分");
        map.put("加料","___时___分");
        map.put("包装",nullDateStr);
        map.put("包装进仓",nullDateStr);
        if (StringUtils.isEmpty(row4)){
            if (row2.equals("检测")){
                if (fenShanIndex == 4){
                    return "___时___分，粘度_______\n细度_______um, 麻点_______um\n ___合格可入仓 , ___不合格 ， ___暂置";
                }else {
                    return "___时___分，粘度_______\n细度_______um, 麻点_______um";

                }

            }else if(row2.contains("研磨")){
                if (jsonObject == null){
                    return nullDateStr;
                }else {
                    String s = jsonObject.getString("接过滤器");
                    if (s != null && s.equals("Y")){
                        return "接过滤器___________\n"+nullDateStr;
                    }else {
                        return nullDateStr;
                    }
                }
            }else {
                return map.get(row2);
            }
        }
        return row4;
    }

    public Map toMap(Map map,String name,String value){
        if (!StringUtils.isEmpty(value)){
            map.put(name,value);
        }else {
            map.put(name,"");
        }
        return map;
    }

}
